<template>
  <rolePrivAlloceView
    :isLoading="isLoading"
    :total="total"
    :pageSize="pageSize"
    :tableData="tableData"
    :outActive="outActive"
    :tabsList="tabsList"
    :menudata="menudata"
    :defaultProps="defaultProps"
    :isSelectRow="isSelectRow"
    :configMenu="configMenu"
    :dataConfigTree="dataConfigTree"
    :strictly="strictly"
    @findRole="findRole"
    @changePage="changePage"
    @sizeChange="sizeChange"
    @rowClickUser="rowClickUser"
    @menuTabsClick="menuTabsClick"
    @menuNodeClick="menuNodeClick"
    @configMenuClose="configMenuClose"
    @configMenuCancel="configMenuCancel"
    @configMenuSure="configMenuSure"
    @configMenuBtn="configMenuBtn"
    ref="role"
  ></rolePrivAlloceView>
</template>

<script>
  import rolePrivAlloceView from './rolePrivAlloceView'

  export default {

    components: {
      rolePrivAlloceView
      // name
    },
    name: "rolePrivAlloceLogic",
    props: {
      // msg: String //  [name: typeof]
    },
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        isLoading: false, // 是否加载状态
        total: 0, // 总条数
        pageSize: 10, // 每页条数
        tableData: [], // 角色列表
        outActive: "",
        tabsList: [],
        menudata: [], // 菜单树
        defaultProps: { // 菜单
          children: 'childs',
          label: 'name'
        },
        dataRoleList: [], // 总数据
        currentPage: 1, // 当前页
        selectRow: {}, // 选中行的信息
        configMenu: false, // 弹窗状态
        isSelectRow: true, // 是否选中了行
        dataConfigTree: [],
        normalConifg: [],
        strictly: true,  // 来控制tree是不是严格模式
      }
    },
    created() {
      this.getRoleList();
      this.getTabsList();
    },
    mounted() {

    },
    methods: {
      // 获取角色
      getRoleList(obj) {
        this.isLoading = true;
        _https.post.getRoleList(obj).then(res => {
          if (res.data.code == 0) {
            let data = res.data.data;
            this.dataRoleList = data;
            let currentPage = this.currentPage;
            if (data.length % this.pageSize == 0 && currentPage != 1) {
              currentPage = Math.ceil(data.length / this.pageSize);
              this.currentPage = currentPage;
            }
            this.setDataList(currentPage);
            this.total = data.length;
            this.isLoading = false;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })

      },
      // 获取tabs（字典管理）
      getTabsList() {
        let obj = {
          datatype: "activity"
        };
        _https.post.getDict(obj).then(res => {
          if (res.data.code == 0) {
            this.outActive = res.data.data[0].datavalue;
            this.tabsList = res.data.data;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })
      },
      /**
       * 将返回的数据进行分页操作
       * @param currentPage {Number}
       */
      setDataList(currentPage) {
        let current = currentPage * this.pageSize;
        let currentStart = current - this.pageSize;
        let currentEnd = current;
        this.tableData = this.dataRoleList.slice(currentStart, currentEnd);
      },
      // 搜索
      findRole(seachVal) {
        this.currentPage = 1;
        let obj = {
          id: "",
          name: seachVal
        };
        if (seachVal == "") {
          this.getRoleList();
        } else {
          this.getRoleList(obj);
        }
      },
      // 分页改变的时候触发
      changePage(page) {
        this.currentPage = page;
        this.setDataList(page);
        this.isSelectRow = true;
      },
      sizeChange(num) {
        this.pageSize = num;
        this.getRoleList();
      },
      // 点击行的回调
      rowClickUser(row, column, event) {
        this.selectRow = row;
        this.isSelectRow = false;
        this.getQueryActivityByRole(row.id, this.outActive);
      },
      // TODO
      getQueryActivityByRole(roleId, type) {
        let obj = {
          roleId: roleId,
          type: type
        };
        _https.post.getQueryActivityByRole(obj).then(res => {
          if (res.data.code == 0) {
            this.menudata = res.data.data;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        });

        _https.post.getQueryActivityByRoleAAndTypeNoTree(obj).then(res => {
          if (res.data.code == 0) {
            this.normalConifg = res.data.data;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })
      },
      // TODO
      getAllMenu(type) {
        let obj = {
          type: type,
          clientId: this.selectRow.clientId
        };
        _https.post.getFindActivitisByTypeeAndClientId(obj).then(res => {
          if (res.data.code == 0) {
            this.dataConfigTree = res.data.data;
            this.configMenu = true;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        }).then(() => {
          this.$refs.role.$refs.tree.setCheckedNodes(this.normalConifg);
          this.strictly = false;
        })
      },
      // 点击tabs的回调
      menuTabsClick(e) {
        this.outActive = e.name;
        if (!this.selectRow.id) return;
        this.getQueryActivityByRole(this.selectRow.id, e.name);
      },
      // 点击树节点的回调
      menuNodeClick(nodeInfo) {

      },
      // 关闭弹窗的回调
      configMenuClose() {
        this.configMenu = false;
        this.strictly = true;
      },
      // 弹窗取消
      configMenuCancel() {
        this.configMenu = false;
        this.strictly = true;
      },
      // TODO
      // 弹窗确定
      configMenuSure() {
        let checkedNodes = this.$refs.role.$refs.tree.getCheckedNodes(false, true);
        let newCheckedNodes = checkedNodes.map(item => item.id);
        let obj = {
          ids: newCheckedNodes.join(','),
          type: this.outActive,
          roleId: this.selectRow.id
        };

        _https.post.getSaveActivityToRole(obj).then(res => {
          if (res.data.code == 0) {
            this.$message({type: 'success', message: res.data.message});
            this.getQueryActivityByRole(this.selectRow.id, this.outActive);
            this.configMenu = false;
            this.strictly = true;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })
      },
      // 点击配置菜单按钮
      configMenuBtn() {
        this.getAllMenu(this.outActive);
      }
    },
    destroyed() {

    }
  }
</script>

<style scoped lang="less">

</style>
